/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.seppiko.chart.controllers;

import org.seppiko.chart.models.BarcodeEntity;
import org.seppiko.chart.models.ControllerEntity;
import org.seppiko.chart.models.MessageEntity;
import org.seppiko.chart.services.OneDimensionalService;
import org.seppiko.chart.utils.JsonUtil;
import org.seppiko.chart.utils.ResponseUtil;
import org.seppiko.chart.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Leonard Woo
 */
@RestController
public class LinearController {

  @Autowired
  private OneDimensionalService service;

  @GetMapping(value = "/code39")
  public ResponseEntity<byte[]> code39GetContentHandleExecution (
      @RequestParam(name = "data", required = false, defaultValue = "SeppikoChart123") String data,
      @RequestParam(value = "size", required = false, defaultValue = "128x64") String size,
      @RequestParam(value = "format", required = false, defaultValue = "png") String format) {

    BarcodeEntity entity = new BarcodeEntity();
    entity.setData( StringUtils.urlConvertUtf8ToIso88591(data) );

    String[] sizes = size.split("x");
    entity.setWidth(Integer.decode(sizes[0]));
    entity.setHeight(Integer.decode(sizes[1]));
    entity.setMargin(1);

    format = StringUtils.urlDecodeWithUtf8(format);
    entity.setNeedBase64(false);
    entity.setNeedText(true);
    if (format.contains("|")) {
      String[] f = format.split("\\|");
      format = f[0];
      entity.setNeedBase64(f[1].equalsIgnoreCase("base64"));
    }
    if (format.equalsIgnoreCase("json")) {
      entity.setEnableJson(true);
      entity.setFormat("png");
    } else {
      entity.setFormat(format.toLowerCase());
    }

    return ResponseUtil.sendResponse( service.code39CHX(entity) );
  }


  @PostMapping(value = "/code39")
  public ResponseEntity<byte[]> code39PostContentHandleExecution(@RequestBody String requestStr) {
    if (StringUtils.isEmpty(requestStr)) {
      return ResponseUtil.sendResponse( 400, MediaType.APPLICATION_JSON,
          JsonUtil.toJson( new MessageEntity(400, "Bad request, because JSON is {}.") ).getBytes() );
    }
    BarcodeEntity entity = JsonUtil.fromJson(requestStr, BarcodeEntity.class);
    return ResponseUtil.sendResponse( service.code39CHX(entity) );
  }

  @GetMapping(value = "/code93")
  public ResponseEntity<byte[]> code93GetContentHandleExecution (
      @RequestParam(name = "data", required = false, defaultValue = "SeppikoChart123") String data,
      @RequestParam(value = "size", required = false, defaultValue = "128x64") String size,
      @RequestParam(value = "format", required = false, defaultValue = "png") String format) {

    BarcodeEntity entity = new BarcodeEntity();
    entity.setData( StringUtils.urlConvertUtf8ToIso88591(data) );

    String[] sizes = size.split("x");
    entity.setWidth(Integer.decode(sizes[0]));
    entity.setHeight(Integer.decode(sizes[1]));
    entity.setMargin(1);

    format = StringUtils.urlDecodeWithUtf8(format);
    entity.setNeedBase64(false);
    entity.setNeedText(true);
    if (format.contains("|")) {
      String[] f = format.split("\\|");
      format = f[0];
      entity.setNeedBase64(f[1].equalsIgnoreCase("base64"));
    }
    if (format.equalsIgnoreCase("json")) {
      entity.setEnableJson(true);
      entity.setFormat("png");
    } else {
      entity.setFormat(format.toLowerCase());
    }

    return ResponseUtil.sendResponse( service.code93CHX(entity) );
  }


  @PostMapping(value = "/code93")
  public ResponseEntity<byte[]> code93PostContentHandleExecution(@RequestBody String requestStr) {
    if (StringUtils.isEmpty(requestStr)) {
      return ResponseUtil.sendResponse( 400, MediaType.APPLICATION_JSON,
          JsonUtil.toJson( new MessageEntity(400, "Bad request, because JSON is {}.") ).getBytes() );
    }
    BarcodeEntity entity = JsonUtil.fromJson(requestStr, BarcodeEntity.class);
    return ResponseUtil.sendResponse( service.code93CHX(entity) );
  }

  @GetMapping(value = "/code128")
  public ResponseEntity<byte[]> code128GetContentHandleExecution (
      @RequestParam(name = "data", required = false, defaultValue = "SeppikoChart123") String data,
      @RequestParam(value = "size", required = false, defaultValue = "128x64") String size,
      @RequestParam(value = "format", required = false, defaultValue = "png") String format) {

    BarcodeEntity entity = new BarcodeEntity();
    entity.setData( StringUtils.urlConvertUtf8ToIso88591(data) );

    String[] sizes = size.split("x");
    entity.setWidth(Integer.decode(sizes[0]));
    entity.setHeight(Integer.decode(sizes[1]));
    entity.setMargin(1);

    format = StringUtils.urlDecodeWithUtf8(format);
    entity.setNeedBase64(false);
    entity.setNeedText(true);
    if (format.contains("|")) {
      String[] f = format.split("\\|");
      format = f[0];
      entity.setNeedBase64(f[1].equalsIgnoreCase("base64"));
    }
    if (format.equalsIgnoreCase("json")) {
      entity.setEnableJson(true);
      entity.setFormat("png");
    } else {
      entity.setFormat(format.toLowerCase());
    }

    return ResponseUtil.sendResponse( service.code128CHX(entity) );
  }


  @PostMapping(value = "/code128")
  public ResponseEntity<byte[]> code128PostContentHandleExecution(@RequestBody String requestStr) {
    if (StringUtils.isEmpty(requestStr)) {
      return ResponseUtil.sendResponse( 400, MediaType.APPLICATION_JSON,
          JsonUtil.toJson( new MessageEntity(400, "Bad request, because JSON is {}.") ).getBytes() );
    }
    BarcodeEntity entity = JsonUtil.fromJson(requestStr, BarcodeEntity.class);
    return ResponseUtil.sendResponse( service.code128CHX(entity) );
  }

  @GetMapping(value = "/itf")
  public ResponseEntity<byte[]> itfGetContentHandleExecution (
      @RequestParam(name = "data", required = false, defaultValue = "1234567890") String data,
      @RequestParam(value = "size", required = false, defaultValue = "128x64") String size,
      @RequestParam(value = "format", required = false, defaultValue = "png") String format) {

    BarcodeEntity entity = new BarcodeEntity();
    entity.setData( StringUtils.urlConvertUtf8ToIso88591(data) );

    String[] sizes = size.split("x");
    entity.setWidth(Integer.decode(sizes[0]));
    entity.setHeight(Integer.decode(sizes[1]));

    format = StringUtils.urlDecodeWithUtf8(format);
    entity.setNeedBase64(false);
    entity.setNeedText(true);
    if (format.contains("|")) {
      String[] f = format.split("\\|");
      format = f[0];
      entity.setNeedBase64(f[1].equalsIgnoreCase("base64"));
    }
    if (format.equalsIgnoreCase("json")) {
      entity.setEnableJson(true);
      entity.setFormat("png");
    } else {
      entity.setFormat(format.toLowerCase());
    }

    ControllerEntity resp = new ControllerEntity();


    if ( !entity.getData().matches("\\d*") || entity.getData().length() % 2 != 0) {
      resp.setStatus(400);
      resp.setMediaType(MediaType.APPLICATION_JSON);
      resp.setData( JsonUtil.toJson(
          new MessageEntity(400, "Data parameter error!!!Please enter a number of even length.") ).getBytes());
    } else {
      resp = service.itfCHX(entity);
    }

    return ResponseUtil.sendResponse( resp );
  }


  @PostMapping(value = "/itf")
  public ResponseEntity<byte[]> itfPostContentHandleExecution(@RequestBody String requestStr) {
    if (StringUtils.isEmpty(requestStr)) {
      return ResponseUtil.sendResponse( 400, MediaType.APPLICATION_JSON,
          JsonUtil.toJson( new MessageEntity(400, "Bad request, because JSON is {}.") ).getBytes() );
    }
    BarcodeEntity entity = JsonUtil.fromJson(requestStr, BarcodeEntity.class);

    if ( !entity.getData().matches("\\d*") || entity.getData().length() % 2 != 0) {
      return ResponseUtil.sendResponse(400, MediaType.APPLICATION_JSON, JsonUtil.toJson(
          new MessageEntity(400, "Data parameter error!!!Please enter a number of even length.") ).getBytes());
    }

    return ResponseUtil.sendResponse( service.itfCHX(entity) );
  }
}
